/**
 * Created by 121 on 2016/4/08.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar',
    'highCharts'
], function ($, common, ready, template, Collection, Model, dialog, calendar, highCharts) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            this.event();
            //this.fetch();
        },
        event: function () {
            var _this = this;
            var success = function (status) {
                $.ajax({
                    url: _global.url.api + 'jd_goods/order/success',
                    type: 'get',
                    data: {status: status},
                    success: function (res) {
                        console.log(res.data.tickInterval);
                        $('#jd-order-data-success').highcharts({
                            chart: {
                                zoomType: 'xy'
                            },
                            title: {
                                text: ''
                            },
                            xAxis: [{
                                status: res.data.status,
                                categories:res.data.created_date
                            }],
                            yAxis: [{ // Primary yAxis
                                labels: {
                                    format: '{value}单',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                title: {
                                    text: '成交笔数',
                                    style: {
                                        color: '#4572A7'
                                    }
                                }
                            }, { // Secondary yAxis
                                title: {
                                    text: '销售额',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                labels: {
                                    format: '{value}元',
                                    style: {
                                        color: '#4572A7'
                                    }
                                },
                                opposite: true
                            }],
                            tooltip: {
                                shared: true
                            },
                            legend: {
                                layout: 'vertical',
                                align: 'right',
                                verticalAlign: 'middle',
                                backgroundColor: '#FFFFFF'
                            },
                            series: [{
                                name: '下单成功销售额',
                                color: '#4572A7',
                                yAxis: 1,
                                type: 'spline',
                                data: res.data.success.order_amount,
                                tooltip: {
                                    valueSuffix: ' 元'
                                }

                            }, {
                                name: '下单成功成交笔数',
                                color: '#89A54E',
                                type: 'spline',
                                data: res.data.success.order_num,
                                tooltip: {
                                    valueSuffix: '单'
                                }
                            }, {
                                name: '下单失败销售额',
                                color: '#4572A7',
                                yAxis: 1,
                                type: 'spline',
                                data: res.data.fail.order_amount,
                                tooltip: {
                                    valueSuffix: '元'
                                }
                            }, {
                                name: '下单失败成交笔数',
                                color: '#89A54E',
                                type: 'spline',
                                data: res.data.fail.order_num,
                                tooltip: {
                                    valueSuffix: '单'
                                }
                            }]
                        });
                    }
                });
            }
            var fail_reasons = function (status) {
                $.ajax({
                    url: _global.url.api + 'jd_goods/order/fail/reasons',
                    type: 'get',
                    data: {status: status},
                    success: function (res) {
                        $('#jd-order-fail-reasons').highcharts({
                            chart: {
                                zoomType: 'xy'
                            },
                            title: {
                                text: ''
                            },
                            xAxis: {
                                categories: res.data.reasons,
                                labels: {
                                    style: {
                                        fontSize: '16px'
                                    }
                                }
                            },
                            yAxis: {
                                labels: {
                                    format: '{value}单',
                                    style: {
                                        color: '#89A54E'
                                    }
                                },
                                title: {
                                    text: '成交笔数',
                                    style: {
                                        color: '#89A54E'
                                    }
                                }
                            },
                            tooltip: {
                                shared: true
                            },
                            legend: {
                                layout: 'vertical',
                                align: 'right',
                                verticalAlign: 'middle',
                                backgroundColor: '#FFFFFF'
                            },
                            series: [{
                                name: '成交笔数',
                                color: '#89A54E',
                                type: 'column',
                                data: res.data.num,
                                tooltip: {
                                    valueSuffix: '单'
                                }
                            }]
                        });
                    }
                });
            }

            $.ajax({
                url: _global.url.api + 'jd_goods/yesterday/sell_data',
                type: 'get',
                success: function (res) {
                    console.log(res);
                    $('#sell-view').append(template('sell-view-tpl', {'data': res.data}))
                }
            });
            success();
            fail_reasons();
            $('#btn-success-status').find('a').on('click', function (e) {
                $(this).siblings().removeClass('active');
                e.preventDefault();
                var status = $(e.target).attr('rel');
                success(status);
            });
            $('#btn-fail-status').find('a').on('click', function (e) {
                $(this).siblings().removeClass('active');
                e.preventDefault();
                var status = $(e.target).attr('rel');
                fail(status);
            });
            $('#btn-fail-reasons-status').find('a').on('click', function (e) {
                $(this).siblings().removeClass('active');
                e.preventDefault();
                var status = $(e.target).attr('rel');
                fail_reasons(status);
            });
        }
    };
    new index();
});